;CP15寄存器相关的操作

   AREA CP15CODE, CODE, READONLY
;  PRESERVE8

;=============================================================
;register 0 related
;=============================================================

;Prototype: 
;   UINT32 CP15_ReadDeviceID(void)
;remark:
;   bit31~bit24:implementer
;   bit24~bit20:specify revision
;   bit19~bit16:arch
;   bit15~bit4:part number
;   bit3~bit0:layout revision
    EXPORT CP15_ReadDeviceID
CP15_ReadDeviceID
    MRC p15,0,r0,c0,c0,0
    mov pc,lr

;Prototype: 
;   UINT32 CP15_ReadCacheInfo(void)
;remark:
;   bit28~bit25: ctype
;   bit24: united cache or seperate instruction or data cache
;   bit23~bit12: data cache size
;   bit11~bit0: instruction data cache
    EXPORT CP15_ReadCacheInfo
CP15_ReadCacheInfo
    mrc p15,0,r0,c0,c0,1
    mov pc,lr

;===========================================================
;register 1 related
;===========================================================
    MACRO
    CR1_BITSET $bit
    mrc p15, 0, r0, c1, c0, 0
    orr r0,r0,$bit
    mcr p15, 0, r0, c1, c0, 0
    mov pc,lr
    MEND

    MACRO
    CR1_BITCLEAR $bit
    mrc p15, 0, r0, c1, c0, 0
    bic r0,r0,$bit
    mcr p15, 0, r0, c1, c0, 0
    mov pc,lr
    MEND

R1_MBIT EQU  (1<<0)
R1_ABIT EQU  (1<<1)
R1_CBIT EQU  (1<<2)
R1_BBIT EQU  (1<<7)
R1_SBIT EQU  (1<<8)
R1_RBIT EQU  (1<<9)
R1_IBIT EQU  (1<<12)
R1_VBIT EQU  (1<<13)
R1_RRBIT EQU (1<<14)
R1_nFBIT EQU (1<<30)
R1_iABIT EQU (1<<31)

;Prototype: 
;   void CP15_EnableMMU(void)
    EXPORT CP15_EnableMMU
CP15_EnableMMU
    CR1_BITSET #R1_MBIT

;Prototype: 
;   void CP15_DisableMMU(void)
    EXPORT CP15_DisableMMU
CP15_DisableMMU
    CR1_BITCLEAR #R1_MBIT

;Prototype: 
;   void CP15_EnableAlignCheck(void)
    EXPORT CP15_EnableAlignCheck
CP15_EnableAlignCheck
    CR1_BITSET #R1_ABIT

;Prototype: 
;   void CP15_DisableAlignCheck(void)
    EXPORT CP15_DisableAlignCheck
CP15_DisableAlignCheck
    CR1_BITCLEAR #R1_ABIT

;Prototype: 
;   void CP15_EnableDCache(void)
    EXPORT CP15_EnableDCache
CP15_EnableDCache
    CR1_BITSET #R1_CBIT

;Prototype: 
;   void CP15_DisableDCache(void)
    EXPORT CP15_DisableDCache
CP15_DisableDCache
    CR1_BITCLEAR #R1_CBIT

;Prototype: 
;   void CP15_SetBigEndian(void)
    EXPORT CP15_SetBigEndian
CP15_SetBigEndian
    CR1_BITSET #R1_BBIT

;Prototype: 
;   void CP15_SetLittleEndian(void)
    EXPORT CP15_SetLittleEndian
CP15_SetLittleEndian
    CR1_BITCLEAR #R1_BBIT

;Prototype: 
;   void CP15_SetSBit(void)
    EXPORT CP15_SetSBit
CP15_SetSBit
    CR1_BITSET #R1_SBIT

;Prototype: 
;   void CP15_ClearSBit(void)
    EXPORT CP15_ClearSBit
CP15_ClearSBit
    CR1_BITCLEAR #R1_SBIT

;Prototype: 
;   void CP15_SetRBit(void)
    EXPORT CP15_SetRBit
CP15_SetRBit
    CR1_BITSET #R1_RBIT

;Prototype: 
;   void CP15_ClearRBit(void)
    EXPORT CP15_ClearRBit
CP15_ClearRBit
    CR1_BITCLEAR #R1_RBIT

;Prototype: 
;   void CP15_EnableICache(void)
    EXPORT CP15_EnableICache
CP15_EnableICache
    CR1_BITSET #R1_IBIT

;Prototype: 
;   void CP15_DisableICache(void)
    EXPORT CP15_DisableICache
CP15_DisableICache
    CR1_BITCLEAR #R1_IBIT

;Prototype: 
;   void CP15_SetVBit(void)
    EXPORT CP15_SetVBit
CP15_SetVBit
    CR1_BITSET #R1_VBIT

;Prototype: 
;   void CP15_ClearVBit(void)
    EXPORT CP15_ClearVBit
CP15_ClearVBit
    CR1_BITCLEAR #R1_VBIT

;Prototype: 
;   void CP15_SetRRBit(void)
    EXPORT CP15_SetRRBit
CP15_SetRRBit
    CR1_BITSET #R1_RRBIT

;Prototype: 
;   void CP15_ClearRRBit(void)
    EXPORT CP15_ClearRRBit
CP15_ClearRRBit
    CR1_BITCLEAR #R1_RRBIT

;Prototype: 
;  void CP15_SetFastBusMode(void)
    EXPORT CP15_SetFastBusMode
CP15_SetFastBusMode
    mrc p15,0,r0,c1,c0,0
    bic r0,r0,#(R1_iABIT:OR:R1_nFBIT)
    mcr p15,0,r0,c1,c0,0
    mov pc,lr

;Prototype: 
;   void CP15_SetAsyncBusMode(void)
    EXPORT CP15_SetAsyncBusMode
CP15_SetAsyncBusMode
    mrc p15,0,r0,c1,c0,0
    orr r0,r0,#(R1_nFBIT:OR:R1_iABIT)
    mcr p15,0,r0,c1,c0,0
    mov pc,lr

;==========================================================
;register2
;==========================================================

;Prototype: 
;   void CP15_SetTTBBase(UINT32 addr)
    EXPORT CP15_SetTTBBase
CP15_SetTTBBase
    mov r1,#0x3f
    lsl r1,#8
    orr r1,r1,#0xff
    bic r0,r0,r1
    mcr p15, 0, r0, c2, c0, 0
    mov pc,lr

;==========================================================
;register3
;==========================================================

;Prototype: 
;   void CP15_SetDomain(UINT32 domain)
    EXPORT CP15_SetDomain
CP15_SetDomain
    ;r0=domain
    mcr p15,0,r0,c3,c0,0
    mov pc,lr

;Prototype: 
;   UINT32 CP15_GetDomain(void)
    EXPORT CP15_GetDomain
CP15_GetDomain
    mrc p15,0,r0,c3,c0,0
    mov pc,lr

;===========================================================
;MVA Address Format
;bit31~bit5         bit4~bit0
;MVA address          00000
;index Format
;bit31~bit26 bit25~bit8 bit7~bit5 bit4~bit0
;   index        0         seg        0
;===========================================================
    EXPORT CP15_InvalidateIDCache
CP15_InvalidateIDCache
    mov r0,#0
    mcr p15,0,r0,c7,c7,0
    mov pc,lr

    EXPORT CP15_InvalidateICache
CP15_InvalidateICache
    mov r0,#0
    mcr p15,0,r0,c7,c5,0
    mov pc,lr

    EXPORT CP15_InvalidateDCache
CP15_InvalidateDCache
    mov r0,#0
    mcr p15,0,r0,c7,c6,0
    mov pc,lr

;void CP15_InvalidICacheMVA(UINT32 mva)
    EXPORT CP15_InvalidICacheMVA
CP15_InvalidICacheMVA
    mcr p15,0,r0,c7,c5,1
    mov pc,lr

;void CP15_PrefetchICacheMVA(UINT32 mva)
    EXPORT CP15_PrefetchICacheMVA
CP15_PrefetchICacheMVA
    mcr p15,0,r0,c7,c13,1
    mov pc,lr

;void CP15_InvalidDCacheMVA(UINT32 mva)
    EXPORT CP15_InvalidDCacheMVA
CP15_InvalidDCacheMVA
    mcr p15,0,r0,c7,c6,1
    mov pc,lr

;void CP15_CleanSingleDCacheEntryMVA(UINT32 entry)
    EXPORT CP15_CleanSingleDCacheEntryMVA
CP15_CleanSingleDCacheEntryMVA
    mcr p15,0,r0,c7,c10,1
    mov pc,lr

;void CP15_CleanAndInvalidateDCacheEntryMVA(UINT32 entry)
    EXPORT CP15_CleanAndInvalidateDCacheEntryMVA
CP15_CleanAndInvalidateDCacheEntryMVA
    mcr p15,0,r0,c7,c14,1
    mov pc,lr

;void CP15_CleanSingleDCacheEntryIdx(UINT32 index)
    EXPORT CP15_CleanSingleDCacheEntryIdx
CP15_CleanSingleDCacheEntryIdx
    mcr p15,0,r0,c7,c10,2
    mov pc,lr

;void CP15_CleanAndInvalidateDCacheEntryIdx(UINT32 index)
    EXPORT CP15_CleanAndInvalidateDCacheEntryIdx
CP15_CleanAndInvalidateDCacheEntryIdx
    mcr p15,0,r0,c7,c14,2
    mov pc,lr

;void CP15_DrainWriteBuffer(void)
    EXPORT CP15_DrainWriteBuffer
CP15_DrainWriteBuffer
    mov r0,#0
    mcr p15,0,r0,c7,c10,4
    mov pc,lr

;void CP15_WaitForInterrupt(void)
    EXPORT CP15_WaitForInterrupt
CP15_WaitForInterrupt
    mov r0,#0
    mcr p15,0,r0,c7,c0,4
    mov pc,lr

;=============================================================
;register 8,TLB 相关的操作
;write-only
;manage instruction TLBs and data TLBs
;
;mva Format
;  bit31~bit10 bit9~bit0
;      mva         0  
;=============================================================

;void CP15_InvalidateTLB(void)
    EXPORT CP15_InvalidateTLB
CP15_InvalidateTLB
    mov r0,#0
    mcr p15,0,r0,c8,c7,0
    mov pc,lr

;void CP15_InvalidateTLB_I(void)
    EXPORT CP15_InvalidateTLB_I
CP15_InvalidateTLB_I
    mov r0,#0
    mcr p15,0,r0,c8,c5,0
    mov pc,lr

;void CP15_InvalidateTLB_I_Entry(UINT32 mva)
    EXPORT CP15_InvalidateTLB_I_Entry
CP15_InvalidateTLB_I_Entry
    ;r0=mva
    mcr p15,0,r0,c8,c5,1
    mov pc,lr

;void CP15_InvalidateTLB_D(void)
    EXPORT CP15_InvalidateTLB_D
CP15_InvalidateTLB_D
    mov r0,#0
    mcr p15,0,r0,c8,c6,0
    mov pc,lr

;void CP15_InvalidateTLB_D_Entry(UINT32 mva)
    EXPORT CP15_InvalidateTLB_D_Entry
CP15_InvalidateTLB_D_Entry
    ;r0=mva
    mcr p15,0,r0,c8,c6,1
    mov pc,lr

;============================================================
;register9,cache lockdown
;read opr: only bit31~bit26 is valid
;write opt: bit0~bit25 should be zero
;============================================================

;UINT32 CP15_ReadDCacheLockdownBase(void)
    EXPORT CP15_ReadDCacheLockdownBase
CP15_ReadDCacheLockdownBase
    mov r0,#0
    mrc p15,0,r0,c9,c0,0
    mov pc,lr

;UINT32 CP15_ReadICacheLockdownBase(void)
    EXPORT CP15_ReadICacheLockdownBase
CP15_ReadICacheLockdownBase
    mov r0,#0
    mrc p15,0,r0,c9,c0,1
    mov pc,lr

;void CP15_WriteDCacheVictimAndBase(UINT32 victimOrBase)
;victim=base
    EXPORT CP15_WriteDCacheVictimAndBase
CP15_WriteDCacheVictimAndBase
    mcr p15,0,r0,c9,c0,0
    mov pc,lr

;void CP15_WriteICacheVictimAndBase(UINT32 victimOrBase)
;victim=base
    EXPORT CP15_WriteICacheVictimAndBase
CP15_WriteICacheVictimAndBase
    mcr p15,0,r0,c9,c0,1
    mov pc,lr


;============================================================
;register10,tlb lockdown
;bit31~bit26   bit25~bit20   bit19~bit1   bit0
;   Base          Victim       SBZ/UNP     P 
;============================================================

;UINT32 CP15_ReadTLBLockDown_D(void)
    EXPORT CP15_ReadTLBLockDown_D
CP15_ReadTLBLockDown_D
    mrc p15,0,r0,c10,c0,0
    mov pc,lr

;UINT32 CP15_ReadTLBLockDown_I(void)
    EXPORT CP15_ReadTLBLockDown_I
CP15_ReadTLBLockDown_I
    mrc p15,0,r0,c10,c0,1
    mov pc,lr

;void CP15_WriteTLBLockDown_I(UINT32 mva)
    EXPORT CP15_WriteTLBLockDown_I
CP15_WriteTLBLockDown_I
    mcr p15,0,r0,c10,c0,1
    mov pc,lr

;void CP15_WriteTLBLockDown_D(UINT32 mva)
    EXPORT CP15_WriteTLBLockDown_D
CP15_WriteTLBLockDown_D
    mcr p15,0,r0,c10,c0,0
    mov pc,lr

;============================================================
;register13,快速上下文切换
;============================================================
;UINT32 CP15_ReadProcessId(void)
    EXPORT CP15_ReadProcessId
CP15_ReadProcessId
    mrc p15,0,r0,c13,c0,0
    mov pc,lr

;void CP15_SetProcessId(UINT32 id)
    EXPORT CP15_SetProcessId
CP15_SetProcessId
    mcr p15,0,r0,c13,c0,0
    mov pc,lr

    END